﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace 桌面视频录制
{
    class VideoCapture
    {
        private string savePath;//文件保存路径(含文件名)
        private int interval;//录制时间间隔

        public int Interval
        {
            set { interval = value; }
        }
        private Rectangle region;//录制区域
        private bool isExit;

        private ImageFormat imageFormat;
        public VideoCapture(string savePath, int interval, Rectangle region, string saveType)
        {
            this.savePath = savePath;
            this.interval = interval;
            this.region = region;
            switch (saveType)
            { 
                case "JPEG":
                    this.imageFormat = ImageFormat.Jpeg;
                    break;
                case "GIF":
                    this.imageFormat = ImageFormat.Gif;
                    break;
                case "PNG":
                    this.imageFormat = ImageFormat.Png;
                    break;
                case "TIFF":
                    this.imageFormat = ImageFormat.Tiff;
                    break;
                case "BMP":
                    this.imageFormat = ImageFormat.Bmp;
                    break;
                default:
                    this.imageFormat = ImageFormat.Jpeg;
                    break;
            }
        }
        public void Start()
        {
            this.isExit = false;
            Thread thread = new Thread(StartCapture);
            thread.IsBackground = true;
            thread.Start();
        }
        private void StartCapture()
        {
            FileStream fs = File.Open(savePath,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);
            BinaryWriter bw = new BinaryWriter(fs);
            if (fs.Length == 0)
                bw.Write(0L);
            else
                fs.Seek(0, SeekOrigin.End);
            long imageCount = 0;//录制的图片的数目
            while (isExit == false)
            {
                //获取桌面的数据
                Bitmap desktopImage = new Bitmap(region.Width, region.Height);
                using (Graphics g = Graphics.FromImage(desktopImage))
                {
                    g.CopyFromScreen(region.Location, new Point(0, 0), desktopImage.Size);
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    desktopImage.Save(ms, imageFormat);
                    byte[] bs = ms.GetBuffer();
                    bw.Write(bs.Length);//写入一侧捕获的数据长度
                    bw.Write(bs);//写入一个捕获的数据
                    bw.Flush();
                    imageCount++;
                    desktopImage.Dispose();
                    desktopImage = null;
                    Thread.Sleep(interval);//停止一段时间间隔的录制
                }
            }
            fs.Seek(0, SeekOrigin.Begin);
            BinaryReader br = new BinaryReader(fs);
            imageCount += br.ReadInt64();
            fs.Seek(0, SeekOrigin.Begin);
            bw.Write(imageCount);
            br.Close();
            bw.Close();
            fs.Close();
        }
        public void Stop()
        {
            this.isExit = true;
        }
    }
}
